package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.dto.CompanyInfoDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.*;
import com.yaojiaxun.logisticsPlatformSystem.service.*;
import com.yaojiaxun.logisticsPlatformSystem.util.MD5Util;
import com.yaojiaxun.logisticsPlatformSystem.util.ResultVOUtil;
import com.yaojiaxun.logisticsPlatformSystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/18 17:14
 * @Description:
 */
@Controller
public class CompanyInfoController {

    @Autowired
    CompanyInfoService companyInfoService;

    @Autowired
    RouteInfoService routeInfoService;

    @Autowired
    PersonInformationService personInformationService;

    @Autowired
    MaterialInfoService materialInfoService;

    @Autowired
    OrderInfoService orderInfoService;

    @RequestMapping("/com_manage")
    public String ComManageIndex(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
        map.put("pageDTO", companyInfoService.findPage(page));
        return "system/com/com_manage";
    }

    @RequestMapping("/com_detail")
    public String getCompanyInfo(@RequestParam("companyId") Long companyId, @RequestParam("page") Integer page, Map<String, Object> map){
        map.put("pageNum", page);
        map.put("comInfo", companyInfoService.findCompanyById(companyId));
        return "system/com/com_detail";
    }

    @RequestMapping("/com_update_after")
    public String updateComAfter(@RequestParam("page") Integer page, CompanyInfo companyInfo){
        companyInfoService.updateCompanyInfo(companyInfo);
        return "redirect:com_manage?page="+page;
    }

    @RequestMapping("/comAddBefore")
    public String addCompanyBefore(){
        return "system/com/com_add";
    }

    @RequestMapping("/com_add_after")
    public String addCompanyAfter(CompanyInfo companyInfo){
        companyInfoService.addCompanyInfo(companyInfo);
        return "redirect:/com_manage";
    }

    @RequestMapping("/comDelete")
    public String deleteCompanyById(@RequestParam("comId") Long comId, @RequestParam("page") Integer page){
        companyInfoService.deleteCompany(comId);
        return "redirect:/com_manage?page="+page;
    }


    //跳转到对应公司的网站(待完善)
    @RequestMapping("/com_detail_material")
    public String findComByName(@RequestParam("companyName") String companyName, Map<String, Object> map){
        Long comId = companyInfoService.findCompanyByName(companyName).getCompanyId();
        map.put("comInfo", companyInfoService.findCompanyById(comId));
        return "system/com/com_detail_other";
    }
    //跳转到对应公司的网站JSON(待完善)
    @ResponseBody
    @RequestMapping("/com_detail_material_json")
    public ResultVO findComByNameJson(@RequestParam("companyName") String companyName){
        Long comId = companyInfoService.findCompanyByName(companyName).getCompanyId();
        return ResultVOUtil.success(companyInfoService.findCompanyById(comId));
    }

    @RequestMapping("/findComByComName")
    public String findComByComName(@RequestParam("comName") String comName, Map<String, Object> map){
        map.put("results", companyInfoService.findCompanyByName(comName));
        return "system/com/com_search_results";
    }

    @RequestMapping("/com_detail_route")
    public String findComByNameRoute(@RequestParam("comName") String comName, Map<String, Object> map){
        Long comId = companyInfoService.findCompanyByName(comName).getCompanyId();
        map.put("comInfo", companyInfoService.findCompanyById(comId));
        return "system/com/com_detail_other";
    }

    //模糊查询公司名
    @RequestMapping("/company_list_search")
    public String findCompanyByNameBlur(@RequestParam(defaultValue = "1") Integer page, @RequestParam("companyName") String companyName,
                                        Map<String, Object> map, CompanySearch companySearch){
        companySearch.setCompanyName(companyName);
        PageDTO<CompanyInfoDTO> pageDTO = companyInfoService.findPage(page, companySearch);
        map.put("companyName", companyName);
        map.put("pageDTO", pageDTO);
        return "front/company/company_list_search";
    }

    //跳转到驻平台公司管理主页
    @RequestMapping("/com_sys_manage")
    public String jumpComSysManage(@RequestParam(required = false) String account, @RequestParam(required = false) String password, HttpSession session, Map<String, Object> map){
        PersonInformation personInformation = personInformationService.findPersonByPersonAccount(account);
        if (personInformation == null){
            //若未登录，则跳转到驻平台公司管理员登录页面
            return "redirect:/sys_com_login";
        }
        if (personInformation.getProperty().equals("管理员") && !personInformation.getBelongedCompany().equals("平台")){
            if (personInformation.getPassword().equals(MD5Util.inputPassToDbPass(password))){
                //session记录登录状态
                session.setAttribute("sys_admin", account);
                //map记录登陆公司信息
                map.put("comInfo", companyInfoService.findCompanyByName(personInformationService.findPersonByPersonAccount(account).getBelongedCompany()));
                map.put("routeInfo", routeInfoService.findRouteByCompany(personInformationService.findPersonByPersonAccount(account).getBelongedCompany()));
                map.put("materialInfo", materialInfoService.findMaterialByCompany(personInformationService.findPersonByPersonAccount(account).getBelongedCompany()));
                map.put("orderInfo", orderInfoService.findOrdersByOrderCom(personInformationService.findPersonByPersonAccount(account).getBelongedCompany()));
                return "system/com/com_sys_manage";
            }
            else
                return "system/sys/login_error";
        }
        else{
            return "system/sys/login_error";
        }
    }

    //驻平台公司管理员修改联系方式栏
    @RequestMapping("/com_sys_update_after_1")
    public String comSysUpdateAfter1(@RequestParam("companyId") Long companyId, @RequestParam("phoneNumber") String phoneNumber, @RequestParam("addressProvince") String addressProvince,
                                     @RequestParam("addressCity") String addressCity, @RequestParam("addressReal") String addressReal,
                                     Map<String, Object> map){
        CompanyInfo companyInfo = companyInfoService.findCompanyById(companyId);
        companyInfo.setPhoneNumber(phoneNumber);
        companyInfo.setAddressProvince(addressProvince);
        companyInfo.setAddressCity(addressCity);
        companyInfo.setAddressReal(addressReal);
        companyInfoService.updateCompanyInfo(companyInfo);
        map.put("comInfo", companyInfoService.findCompanyById(companyId));
        map.put("routeInfo", routeInfoService.findRouteByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("materialInfo",materialInfoService.findMaterialByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("orderInfo", orderInfoService.findOrdersByOrderCom(companyInfoService.findCompanyById(companyId).getCompanyName()));
        return "system/com/com_sys_manage";
    }
    //驻平台公司管理员修改公司简介
    @RequestMapping("/com_sys_update_after_2")
    public String comSysUpdateAfter2(@RequestParam("companyId") Long companyId, @RequestParam("introduction") String introduction,
                                     Map<String, Object> map){
        CompanyInfo companyInfo = companyInfoService.findCompanyById(companyId);
        companyInfo.setIntroduction(introduction);
        companyInfoService.updateCompanyInfo(companyInfo);
        map.put("comInfo", companyInfoService.findCompanyById(companyId));
        map.put("routeInfo", routeInfoService.findRouteByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("materialInfo", materialInfoService.findMaterialByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("orderInfo", orderInfoService.findOrdersByOrderCom(companyInfoService.findCompanyById(companyId).getCompanyName()));
        return "system/com/com_sys_manage";
    }
    //驻平台公司管理员修改物流专线（跳转到新页面专门修改物流专线）
    @RequestMapping("/route_xiugai")
    public String routeXiugai(@RequestParam("routeId") Long routeId, Map<String, Object> map){
        map.put("routeInfo", routeInfoService.findRouteInfo(routeId));
        return "system/route/route_xiugai";
    }
    //驻平台公司管理员修改物流专线完成（）
    @RequestMapping("/route_xiugai_after")
    public String routeXiugaiAfter(RouteInfo routeInfo){
        routeInfoService.updateRouteInfo(routeInfo);
        return "system/route/route_xiugai";
    }
    //驻平台公司管理员添加物流专线
    @RequestMapping("/route_tianjia")
    public String routeTianjia(@RequestParam("companyId") Long companyId, Map<String, Object> map){
        map.put("com",companyInfoService.findCompanyById(companyId));
        return "system/route/route_tianjia";
    }
    //驻平台公司管理员添加物流专线完成（）
    @RequestMapping("/route_tianjia_after")
    public String routeTianjiaAfter(@RequestParam("companyId") Long companyId, RouteInfo routeInfo, Map<String, Object> map){
        map.put("com", companyInfoService.findCompanyById(companyId));
        routeInfoService.addRouteInfo(routeInfo);
        return "system/route/route_tianjia";
    }
    //驻平台公司管理员删除物流专线
    @RequestMapping("/route_shanchu")
    public String routeShanchu(@RequestParam("routeId") Long routeId, @RequestParam("companyId") Long companyId,Map<String, Object> map){
        routeInfoService.deleteRouteInfo(routeId);
        map.put("comInfo", companyInfoService.findCompanyById(companyId));
        map.put("routeInfo", routeInfoService.findRouteByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("materialInfo", materialInfoService.findMaterialByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("orderInfo", orderInfoService.findOrdersByOrderCom(companyInfoService.findCompanyById(companyId).getCompanyName()));
        return "system/com/com_sys_manage";
    }
    //驻平台公司管理员查看/修改货源信息
    @RequestMapping("/material_xiugai")
    public String materialXiugai(@RequestParam("materialId") Long materialId, Map<String, Object> map){
        map.put("materialInfo" ,materialInfoService.findMaterialById(materialId));
        return "system/material/material_xiugai";
    }
    //驻平台公司管理员查看/修改货源信息完成()
    @RequestMapping("/material_xiugai_after")
    public String materialXiugaiAfter(MaterialInfo materialInfo){
        materialInfoService.updateMaterialByMaterialId(materialInfo);
        return "system/material/material_xiugai";
    }
    //驻平台公司管理员删除货源信息
    @RequestMapping("/material_shanchu")
    public String materialShanchu(@RequestParam("materialId") Long materialId, @RequestParam("companyId") Long companyId, Map<String, Object> map){
        materialInfoService.deleteMaterial(materialId);
        map.put("comInfo", companyInfoService.findCompanyById(companyId));
        map.put("routeInfo", routeInfoService.findRouteByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("materialInfo", materialInfoService.findMaterialByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("orderInfo",orderInfoService.findOrdersByOrderCom(companyInfoService.findCompanyById(companyId).getCompanyName()));
        return "system/com/com_sys_manage";
    }
    //驻平台公司管理员添加货源信息
    @RequestMapping("/material_tianjia")
    public String materialTianjia(@RequestParam("companyId") Long companyId, Map<String, Object> map){
        map.put("com",companyInfoService.findCompanyById(companyId));
        return "system/material/material_tianjia";
    }
    //驻平台公司管理员添加货源信息完成
    @RequestMapping("/material_tianjia_after")
    public String materialTianjiaAfter(@RequestParam("companyId") Long companyId, MaterialInfo materialInfo, Map<String, Object> map){
        map.put("com", companyInfoService.findCompanyById(companyId));
        materialInfoService.insertMaterialInfo(materialInfo);
        return "system/material/material_tianjia";
    }
    //驻平台公司管理员拒绝订单
    @ResponseBody
    @RequestMapping("/order_jujue")
    public ResultVO orderShanchu(@RequestParam("orderId") Long orderId, @RequestParam("companyId") Long companyId, @RequestParam("theResponse") String theResponse, Map<String, Object> map){
//        orderInfoService.deleteOrderInfo(orderId);
//        map.put("comInfo", companyInfoService.findCompanyById(companyId));
//        map.put("routeInfo", routeInfoService.findRouteByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
//        map.put("materialInfo", materialInfoService.findMaterialByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
//        map.put("orderInfo", orderInfoService.findOrdersByOrderCom(companyInfoService.findCompanyById(companyId).getCompanyName()));
//        return "system/com/com_sys_manage";
        OrderInfo orderInfo = orderInfoService.findOrderByOrderId(orderId);
        orderInfo.setOrderStatus("被拒绝");
        orderInfo.setOrderReason(theResponse);
        orderInfoService.updateOrderInfo(orderInfo);
        return ResultVOUtil.success();
    }

    //驻平台公司管理员修改订单状态(可以将未接单订单转为已接单，点击后不可逆)
    @RequestMapping("/order_status_done")
    public String saveOrder(@RequestParam("orderId") Long orderId, @RequestParam("companyId") Long companyId, Map<String, Object> map, HttpSession session){
        OrderInfo orderInfo = orderInfoService.findOrderByOrderId(orderId);
        if (orderInfo.getOrderStatus().equals("未接单")){
            orderInfo.setOrderStatus("已接单");
            orderInfoService.updateOrderInfo(orderInfo);
        }
        else if (orderInfo.getOrderStatus().equals("已接单")){
            orderInfo.setOrderStatus("已接单");
            orderInfoService.updateOrderInfo(orderInfo);
        }
        map.put("comInfo", companyInfoService.findCompanyById(companyId));
        map.put("routeInfo", routeInfoService.findRouteByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("materialInfo", materialInfoService.findMaterialByCompany(companyInfoService.findCompanyById(companyId).getCompanyName()));
        map.put("orderInfo", orderInfoService.findOrdersByOrderCom(companyInfoService.findCompanyById(companyId).getCompanyName()));
        return "system/com/com_sys_manage";
//        return "redirect:/com_sys_manage";
    }
}
